package com.ambow.servlet;

import java.io.IOException;

import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder.In;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;

import com.ambow.Entity.Category;
import com.ambow.Entity.DeliverToStudent;
import com.ambow.Entity.Level;
import com.ambow.Entity.Major;
import com.ambow.Entity.Operator;
import com.ambow.Entity.PackageBook;
import com.ambow.Entity.Stock;
import com.ambow.Entity.Student;
import com.ambow.Entity.TextBook;
import com.ambow.service.DeliverToStudentService;
import com.ambow.service.PackageService;
import com.ambow.service.StockService;
import com.ambow.service.TextBookService;
import com.ambow.service.impl.DeliverToStudentServiceImpl;
import com.ambow.service.impl.PackageServiceImpl;
import com.ambow.service.impl.StockServiceImpl;
import com.ambow.service.impl.TextBookServiceImpl;
import com.ambow.utils.BaseDAO;
import com.ambow.utils.HibernateUtil;


/**
 * Servlet implementation class PackageBookServlet
 */
@WebServlet("/DeliverToStudentServlet")
public class DeliverToStudentServlet extends HttpServlet {
	Session session=HibernateUtil.getSession();
	PackageService pService=new PackageServiceImpl();
	TextBookService tService=new TextBookServiceImpl();
	DeliverToStudentService dService=new DeliverToStudentServiceImpl();
	StockService stockService=new StockServiceImpl();
	Date date=new Date();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String status=request.getParameter("status");
		if (status.equals("addDeliverToStudent")) {
			addDeliverToStudent(request, response);// 学生领书
		} else if (status.equals("queryDeliverToStudent")) {
			queryDeliverToStudent(request, response);// 学生领书查询
		} else if (status.equals("queryDetialBook")) {
			queryDetialBook(request, response);// 查询套书的具体信息
		}
	}
private void addDeliverToStudent(HttpServletRequest request, HttpServletResponse response) {
		// 学生领书
	try {
		Integer sid = Integer.valueOf(request.getParameter("student_id"));// 学生id
		Integer pid = Integer.valueOf(request.getParameter("packageBook_id"));// 套书id
		Student student = dService.queryById(Student.class, sid);
		PackageBook packageBook = pService.queryById(PackageBook.class, pid);
		Integer tid = packageBook.getTextBook().getId();
		System.out.println(tid+"1233454ssdffdg");
		DeliverToStudent deliverToStudent = new DeliverToStudent();
		if(request.getSession().getAttribute("op")!=null) {
			System.out.println("123456778");
			deliverToStudent.setOperator((Operator)request.getSession().getAttribute("op"));
		}else {
			System.out.println("12345678dfgggggg");
			deliverToStudent.setOperator((Operator)BaseDAO.get(Operator.class, 1));
		}
		deliverToStudent.setDeliverTime(date);
		deliverToStudent.setPackageBook(packageBook);
		deliverToStudent.setStudent(student);
		boolean b = dService.addDeliverToStudent(deliverToStudent);
		if (b) {
			System.out.println("学生领书成功");
			Integer cid=Integer.valueOf(request.getParameter("cell"));//货架id
			Stock stock=stockService.queryStock("from Stock as stock where stock.textBook.id="+tid+"and stock.cell.id="+cid);
			System.out.println(stock+"2343567ffgh");
			stock.setAmount(stock.getAmount()-1);
			boolean c = stockService.update(stock);
			System.out.println(c+"123445657");
				if (c) {
					System.out.println("库存数量修改成功");
					request.getRequestDispatcher("pages/module/storage/storage001/student/deliver_query.jsp")
							.forward(request, response);
				} else {
					System.out.println("库存数量修改失败");
	
				}
			request.getRequestDispatcher("pages/module/storage/storage001/student/deliver_query.jsp")
					.forward(request, response);
		} else {
			System.out.println("学生领书失败");
			response.sendRedirect("pages/module/storage/storage001/student/query_package.jsp");
		}
	} catch (Exception e) {
	}
}
		

private void queryDetialBook(HttpServletRequest request, HttpServletResponse response) {
		// 套数详细查询
	try {
		Integer pid = Integer.valueOf(request.getParameter("id"));// 套书id
		PackageBook packageBook = pService.queryById(PackageBook.class, pid);
		request.setAttribute("packageBook", packageBook);
		request.getRequestDispatcher("pages/module/storage/storage001/student/bookInfo_detil.jsp").forward(request,
				response);
		} catch (Exception e) {
		}
}
	private void queryDeliverToStudent(HttpServletRequest request, HttpServletResponse response) {
		// 学生领书查询
		try {
			String deliverDatefrm=request.getParameter("deliverDatefrm");//出库时间
			String deliverDateto=request.getParameter("deliverDateto");//出库时间
			String hql="from DeliverToStudent  ds where (ds.deliverTime between ? and ?)";		
			String []param= {deliverDatefrm,deliverDateto};
			List<DeliverToStudent> dToStudents=dService.queryDeliverToStudent(hql,param);
			if(dToStudents != null && !dToStudents.isEmpty()) {
				request.setAttribute("dToStudents", dToStudents);
				request.getRequestDispatcher("pages/module/storage/storage001/student/deliver_query.jsp").forward(request, response);
			}else {
				response.sendRedirect("pages/module/storage/storage001/student/deliver_query.jsp");
			}
		}catch(Exception e){
			
		}
		
		
	} 
}
